package com.xyht.sca_s.student_manage_system.modules.user.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.annotation.RequestPass;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.user.entity.ChangeUserPassword;
import com.xyht.sca_s.student_manage_system.modules.user.entity.CodeEntity;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.entity.VerifyCode;
import com.xyht.sca_s.student_manage_system.modules.user.entity.req.SmsUserLoginReq;
import com.xyht.sca_s.student_manage_system.modules.user.entity.req.WxLoginReq;
import com.xyht.sca_s.student_manage_system.modules.user.entity.req.WxMpMessagePushReq;
import com.xyht.sca_s.student_manage_system.modules.user.service.SmsUserService;
import com.xyht.sca_s.student_manage_system.modules.user.util.SMSUtil;
import com.xyht.sca_s.student_manage_system.modules.user.util.verify_code.VerifyCodeUtil;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2022-11-27
 */
@RestController
@RequestMapping("/user")
@SecretBody
@Slf4j
public class SmsUserController {
    @Autowired
    private SmsUserService smsUserService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private VerifyCodeUtil verifyCodeUtil;
    @Autowired
    private CacheUtil cacheUtil;

    @ApiOperation(value = "用户登录")
    @PostMapping("/webLogin")
    public ResponseResult webLogin (@RequestBody SmsUserLoginReq smsUserLoginReq) {
        return smsUserService.webLogin(smsUserLoginReq);
    }

    @ApiOperation(value = "微信账号登录")
    @PostMapping("/wxUserLogin")
    public ResponseResult wxLogin (@RequestBody SmsUserLoginReq smsUserLoginReq) {
        return smsUserService.wxLogin(smsUserLoginReq);
    }

    @ApiOperation(value = "个人信息")
    @GetMapping("/personalInfo")
    public ResponseResult getPersonInfo (HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsUserService.getPersonInfo(user_id);
    }

    @ApiOperation(value = "用户信息")
    @GetMapping("/userInfo")
    public ResponseResult getUserInfo (HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsUserService.getUserInfo(user_id);
    }

    @ApiOperation(value = "微信自动登录")
    @PostMapping("/wxLogin")
    public ResponseResult wxLoginCheck (@RequestBody WxLoginReq wxLoginReq) {
        return smsUserService.wxLoginCheck(wxLoginReq);
    }

    @ApiOperation(value = "用户绑定微信")
    @PostMapping("/wxBind")
    public ResponseResult wxBindUser (@RequestBody WxLoginReq wxLoginReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsUserService.wxBindUser(wxLoginReq,user_id);
    }

    @ApiOperation(value = "用户解绑微信")
    @PostMapping("/wxUnBind")
    public ResponseResult wxUnBindUser (@RequestBody WxLoginReq wxLoginReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsUserService.wxUnBindUser(wxLoginReq,user_id);
    }

    @ApiOperation(value = "修改用户头像")
    @PostMapping("/updateUserPic")
    public ResponseResult updateUserPic (@RequestBody SmsUser user, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsUserService.updateUserPic(user_id,user.getUserPic());
    }

    @ApiOperation(value = "获取修改密码验证码")
    @GetMapping("/password/verifyCode")
    public ResponseResult phoneVerifyCode (@RequestParam("phone_num") String phone_num) {
        return smsUserService.phoneVerifyCode(phone_num);
    }

    @ApiOperation(value = "搜索简略用户信息")
    @GetMapping("/search")
    public ResponseResult searchUser (@RequestParam("search_str") String search_str) {
        return smsUserService.searchUser(search_str);
    }

    @ApiOperation(value = "手机验证码修改密码")
    @PostMapping("/password/codeChange")
    @RequestPass
    public ResponseResult userChangePasswordByVerifyCode (@RequestBody ChangeUserPassword entity) {
        return smsUserService.userChangePasswordByVerifyCode(entity);
    }

    @ApiModelProperty(value = "原密码修改密码")
    @PostMapping("/password/oldChange")
    public ResponseResult userChangePasswordByOldPassword (@RequestBody ChangeUserPassword entity, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsUserService.userChangePasswordByOldPassword(user_id,entity);
    }

    @ApiOperation(value = "微信公众号消息推送")
    @PostMapping("/wxMpMessagePush")
    public ResponseResult wxMpMessagePushSwitch (@RequestBody WxMpMessagePushReq entity, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsUserService.wxMpMessagePushSwitch(entity,user_id);
    }

    @ApiOperation("获取验证码")
    @GetMapping("/verifyCode")
    @RequestPass
    public ResponseResult verifyCode() {
        log.debug("获取验证码");
        Map<String, Object> map = new HashMap<>();
        CodeEntity codeEntity = new CodeEntity();

        try {
            //设置长宽
            VerifyCode verifyCode = verifyCodeUtil.generateImg(80, 28);
            String code = verifyCode.getCode();
            String base64Code = Base64.getEncoder().encodeToString(verifyCode.getImgBytes());
            String uid = String.valueOf(new Date().getTime());
            codeEntity.setCode(code);
            codeEntity.setUid(uid);
            codeEntity.setCreate(new Date());


            //存验证码
            cacheUtil.add(uid,code,1, TimeUnit.MINUTES);

            map.put("img", base64Code);
            map.put("uid", uid);
            map.put("verifyCode", code);

        } catch (Exception e) {
            e.printStackTrace();
           return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return CommonResult.success(map);
    }
    @ApiOperation("获取验证码")
    @GetMapping("/code")
    @RequestPass
    public ResponseResult code(@RequestParam("username") String username,
                               @RequestParam("tel") String tel) {
        String result = SMSUtil.SendAdmission(tel,username);
        log.debug("++++++++++++++"+result);
        return CommonResult.success();
    }
}

